﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace MSAcademic.v2.Infrastructure.Utility
{
    public class Text
    {
        public static string ToUrlFriendlyString(string str)
        {
            const string whiteSpaceReplacement = "-";

            var preparedStr = Text.RemoveDiacriticalChars(str.Trim());

            var whitespaceReplacedStr =
                new StringBuilder(preparedStr)
                    .Replace(Environment.NewLine, whiteSpaceReplacement)
                    .Replace("  ", whiteSpaceReplacement)
                    .Replace(" ", whiteSpaceReplacement)
                    .Replace("\t", whiteSpaceReplacement)
                    .ToString();

            var specialCharsReplacedString = Regex.Replace(whitespaceReplacedStr, "[^a-zA-Z0-9-]", string.Empty);

            return specialCharsReplacedString;
        }

        public static string RemoveDiacriticalChars(string str)
        {
            var dict = new Dictionary<char, char>
            {
                {'Č', 'C'}, {'č', 'c'},
                {'Ć', 'C'}, {'ć', 'c'},
                {'Ž', 'Z'}, {'ž', 'z'},
                {'Š', 'S'}, {'š', 's'},
                {'Đ', 'D'}, {'đ', 'd'},
            };

            var sb = new StringBuilder(str);
            foreach (var entry in dict) { sb.Replace(entry.Key, entry.Value); }
            return sb.ToString();
        }
        public bool CheckForImageExtenstions(string url)
        {
            var match = Regex.Match(url, @"^.*\.(jpg|JPG|gif|GIF|jpeg|JPEG|png|PNG)$", RegexOptions.None);
            
            return match.Success;
        }
        public bool IsUrlValid(string url)
        {
            Uri test;
            return (CheckForImageExtenstions(url) && Uri.TryCreate(url, UriKind.Absolute, out test) && (test.Scheme == "http" || test.Scheme == "https"));
        }

        public bool IsValidEmail(string str)
        {
            if (str == null) { return false; }

            return Regex.IsMatch(str,
                   @"^(?("")("".+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +
                   @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");
        }
    }
}
